package org.apache.servicecomb.fence.resource;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import jakarta.persistence.EntityManagerFactory;

@Configuration
public class JpaConfig {

  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource);
    emf.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());
    emf.setPackagesToScan("org.apache.servicecomb.fence.resource");

    Properties jpaProperties = new Properties();
    jpaProperties.setProperty("eclipselink.target-database", "org.eclipse.persistence.platform.database.PostgreSQLPlatform");
    jpaProperties.setProperty("jakarta.persistence.jdbc.url", "jdbc:opengauss://113.45.218.121:10000/postgres?currentSchema=class");
    jpaProperties.setProperty("jakarta.persistence.jdbc.user", "root");
    jpaProperties.setProperty("jakarta.persistence.jdbc.password", "wsm@123456");
    jpaProperties.setProperty("jakarta.persistence.jdbc.driver", "org.opengauss.Driver");
    emf.setJpaProperties(jpaProperties);

    return emf;
  }

  @Bean
  public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
    return new JpaTransactionManager(emf);
  }
}